const db = require("../database/models");
const Drugs_use_log = db.drugs_use_log;
const Op = db.Sequelize.Op;

const Drugs_baseinfo = db.drugs_baseinfo;

// Retrieve all from the database.
exports.findAll = (req, res) => {
    const offset = req.query.offset?parseInt(req.query.offset):0;
    const limit = req.query.limit?parseInt(req.query.limit):10;
    const all = req.query.mode==='all'?true:false;
    const hiden = req.query.mode==='hiden'?true:false;
    const mode = all||hiden?false:true;
    const condition = hiden
    ?{
      'deletedAt':{
        [Op.not]:null
      }  
    }
    :{};
  
     Drugs_use_log.findAndCountAll({
         offset: offset, limit: limit ,paranoid:mode,where:condition,raw:true
        })
      .then(async (result) => {
        //res.send(data.rows[0].drug_list);
        //console.log(JSON.parse(data.rows[0].drug_list));
        var data = result.rows;
        //console.log(data);
        var datas = data;
        
        for(let index in data){
            let obj = data[index];
            //console.log(obj); 
            const drug_list_s =  JSON.parse(obj.drug_list);
            //console.log(drug_list_s);
            let obj_list = [];
            let i = 0;
            for(let item of drug_list_s){
                //console.log(item);
                await Drugs_baseinfo.findByPk(item,{
                    paranoid:mode,
                    raw:true
                }).then((dataset)=>{
                    //console.log(dataset);
                    obj_list[i]=dataset;
                    i++;
                }).catch(err => {
                    res.status(500).send({
                      message:
                        err.message || "Some error occurred while retrieving drugs_baseinfo."
                    });
                  });
                
            }
            datas[index].drug_list = obj_list;
        }

        res.send({
            count:datas.length,
            rows:datas
        });
      })
      .catch(err => {
        res.status(500).send({
          message:
            err.message || "Some error occurred while retrieving drugs_use_log."
        });
      });
    
  };
  
  // Retrieve a single one with attr and value
  exports.findByAttr = (req, res) => {
    const offset = req.query.offset?parseInt(req.query.offset):0;
    const limit = req.query.limit?parseInt(req.query.limit):10;
    const all = req.query.mode==='all'?true:false;
    const hiden = req.query.mode==='hiden'?true:false;
    const mode = all||hiden?false:true;
  
    const attr = req.params.attr;
    const value = req.params.value;
    const condition = {};
    condition[attr]=value;
    if(!all){
      condition['deletedAt']=hiden
      ?{
          [Op.not]:null 
      }
      :null;
    };
    
    Drugs_use_log.findAndCountAll({ 
      where: condition , offset:offset,limit:limit,paranoid:mode,raw:true
    }).then(async data => {
        var data = result.rows;
        //console.log(data);
        var datas = data;
        
        for(let index in data){
            let obj = data[index];
            //console.log(obj); 
            const drug_list_s =  JSON.parse(obj.drug_list);
            //console.log(drug_list_s);
            let obj_list = [];
            let i = 0;
            for(let item of drug_list_s){
                //console.log(item);
                await Drugs_baseinfo.findByPk(item,{
                    paranoid:mode,
                    raw:true
                }).then((dataset)=>{
                    //console.log(dataset);
                    obj_list[i]=dataset;
                    i++;
                }).catch(err => {
                    res.status(500).send({
                      message:
                        err.message || "Some error occurred while retrieving drugs_baseinfo."
                    });
                  });
                
            }
            datas[index].drug_list = obj_list;
        }

        res.send({
            count:datas.length,
            rows:datas
        });
        

      })
      .catch(err => {
        res.status(500).send({
          message:
            err.message || "Some error occurred while retrieving drugs_use_log."
        });
      });
    
  };
  
  // Find a single one with an id
  exports.findOne = (req, res) => {
      const id = req.params.id;
      const mode = req.query.mode=='all'?false:true;

      Drugs_use_log.findByPk(id,{
        paranoid:mode,raw:true})
        .then(async data => {
          //res.send(data);
          console.log('test');
          var datas = data;
 
          const drug_list_s =  JSON.parse(data.drug_list);
          //console.log(drug_list_s);
          let obj_list = [];
          let i = 0;
          for(let item of drug_list_s){
              //console.log(item);
              await Drugs_baseinfo.findByPk(item,{
                  paranoid:mode,
                  raw:true
              }).then((dataset)=>{
                  //console.log(dataset);
                  obj_list[i]=dataset;
                  i++;
              }).catch(err => {
                  res.status(500).send({
                    message:
                      err.message || "Some error occurred while retrieving drugs_baseinfo."
                  });
                });
              
          }
          datas.drug_list = obj_list;
          res.send(datas);

        })
        .catch(err => {
          res.status(500).send({
            message: "Error retrieving Drugs_use_log with id=" + id
          });
        });
    
  };
  